package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BotFileDown;
import com.ruoyi.system.service.IBotFileDownService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资源下载Controller
 * 
 * @author ruoyi
 * @date 2024-09-27
 */
@RestController
@RequestMapping("/system/down")
public class BotFileDownController extends BaseController
{
    @Autowired
    private IBotFileDownService botFileDownService;

    /**
     * 查询资源下载列表
     */
    @PreAuthorize("@ss.hasPermi('system:down:list')")
    @GetMapping("/list")
    public TableDataInfo list(BotFileDown botFileDown)
    {
        startPage();
        List<BotFileDown> list = botFileDownService.selectBotFileDownList(botFileDown);
        return getDataTable(list);
    }

    /**
     * 导出资源下载列表
     */
    @PreAuthorize("@ss.hasPermi('system:down:export')")
    @Log(title = "资源下载", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BotFileDown botFileDown)
    {
        List<BotFileDown> list = botFileDownService.selectBotFileDownList(botFileDown);
        ExcelUtil<BotFileDown> util = new ExcelUtil<BotFileDown>(BotFileDown.class);
        util.exportExcel(response, list, "资源下载数据");
    }

    /**
     * 获取资源下载详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:down:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(botFileDownService.selectBotFileDownById(id));
    }

    /**
     * 新增资源下载
     */
    @PreAuthorize("@ss.hasPermi('system:down:add')")
    @Log(title = "资源下载", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BotFileDown botFileDown)
    {
        String image = botFileDown.getImage();
        if (image != null && image.trim().length()>0 && !image.startsWith("http")) {
            image = RuoYiConfig.getBase() + image;
            botFileDown.setImage(image);
        }
        return toAjax(botFileDownService.insertBotFileDown(botFileDown));
    }

    /**
     * 修改资源下载
     */
    @PreAuthorize("@ss.hasPermi('system:down:edit')")
    @Log(title = "资源下载", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BotFileDown botFileDown)
    {
        String image = botFileDown.getImage();
        if (image != null &&image.trim().length()>0 && !image.startsWith("http")) {
            image = RuoYiConfig.getBase() + image;
            botFileDown.setImage(image);
        }
        // https://ry.ai66.tech/prod-api/common/down/resource?resource=/profile/upload/2024/11/29/20241129232051A001.pdf
        String url = botFileDown.getUrl();
        if (StringUtils.hasText(url) && url.startsWith("https://ry.ai66.tech/prod-api/profile")) {
            if (url.endsWith(".pdf")) {
                int pos = "https://ry.ai66.tech/prod-api".length();
                String downloadUrl = "https://ry.ai66.tech/prod-api/downloadResource?resource=" + url.substring(pos);
                botFileDown.setDownloadUrl(downloadUrl);
            } else {
                botFileDown.setDownloadUrl(url);
            }
        }
        return toAjax(botFileDownService.updateBotFileDown(botFileDown));
    }

    /**
     * 删除资源下载
     */
    @PreAuthorize("@ss.hasPermi('system:down:remove')")
    @Log(title = "资源下载", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(botFileDownService.deleteBotFileDownByIds(ids));
    }
}
